package com.juzhi.dvrapp.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.bigkoo.pickerview.TimePickerView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import com.juzhi.dvrapp.R;
import com.juzhi.dvrapp.bean.Device;
import com.juzhi.dvrapp.manager.DeviceManager;
import com.juzhi.dvrapp.utils.Config;
import com.juzhi.dvrapp.widget.SphericalUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class DeviceTrackActivity extends BaseAcitivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private Device device;
    private SimpleDateFormat format;
    private TimePickerView pvTime;


    @Override
    public int getLayoutId() {
        return R.layout.activity_device_track;
    }

    @SuppressLint("SimpleDateFormat")
    @Override
    protected void initView() {
        initHeaderView();
        setFunctionImg(ContextCompat.getDrawable(this,R.drawable.ic_device_calendar));

        format = new SimpleDateFormat("yyyy-MM-dd");
        String dataTime = this.format.format(new Date());
        setTitleText(dataTime);

        initTimePicker(); //  初始化时间选择器


        String ssid = getIntent().getStringExtra(Config.SSID);
        DeviceManager deviceManager = DeviceManager.getDeviceManager(this);
        device = deviceManager.getDevice(ssid);


        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);



    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        ArrayList<LatLng> triangle = new ArrayList<>();
        triangle.add(new LatLng(22.582200, 113.925992));  // Should match last point
        triangle.add(new LatLng(22.582018, 113.926235));
        triangle.add(new LatLng(22.582000, 113.927231));
        triangle.add(new LatLng(22.581124, 113.927521));
        triangle.add(new LatLng(22.581104, 113.928624));
        triangle.add(new LatLng(22.581024, 113.923521));
        triangle.add(new LatLng(22.581001, 113.922140));  // Should match first point

        mMap.addPolyline(new PolylineOptions()
                .addAll(triangle)
                .color(Color.BLACK));

        double rotation = SphericalUtil.computeAngleBetween(triangle.get(triangle.size() - 1), triangle.get(triangle.size() - 2));
        mMap.addMarker(new MarkerOptions()
                .position(triangle.get(triangle.size()-1))
                .title("车辆位置")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.mark))
                .anchor((float) 0.5,(float) 0.5)
                .rotation((float) rotation));

        if (device.getLatLng()!=null) {
            //mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));  打标记
            mMap.addCircle(new CircleOptions()
                    .center(device.getLatLng())
                    .radius(device.getFenceRadius())
                    .strokeWidth(5)  //圆圈线的宽
                    .strokeColor(getResources().getColor(R.color.colorAccent))   //圆圈线的颜色
                    .fillColor(getResources().getColor(R.color.map_circle_fill))     //圆圈内填充的颜色
            );

            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(22.582200, 113.925992), 15));
        }else {
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(22.582200, 113.925992), 15));
        }

    }

    @Override
    protected void function() {
        super.function();
        pvTime.show();
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        return format.format(date);
    }

    private void initTimePicker() {
        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();

        Calendar startDate = Calendar.getInstance();
        startDate.set(2013, 0, 23);

        Calendar endDate = Calendar.getInstance();
        endDate.set(2019, 11, 28);
        //时间选择器
        pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null

                /*btn_Time.setText(getTime(date));*/
//                TextView btn = (TextView) v;
//                btn.setText(getTime(date));
                setTitleText(getTime(date));
            }
        })
                .setType(new boolean[]{true,true,true,false,false,false})
                .setLabel("", "", "", "", "", "") //设置空字符串以隐藏单位提示   hide label
//                .setDividerColor(R.color.cutting_line)
                .setContentSize(20)
                .setTitleText("日期选择")
                .isCyclic(true)
                .isDialog(true)  //设置显示为Dialog
                .setDate(selectedDate)
                .setLineSpacingMultiplier(2.0f)
                .setRangDate(startDate, endDate)
                .setOutSideCancelable(false)
                .build();

    }
}
